package edu.yctc.weblog.controller.rest.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.constant.URLConstant;
import edu.yctc.weblog.controller.rest.CommentRestController;
import edu.yctc.weblog.entity.CommentDO;
import edu.yctc.weblog.entity.PageDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.enums.CommentDeleteStatus;
import edu.yctc.weblog.enums.CommentStatus;
import edu.yctc.weblog.service.CommentService;
import edu.yctc.weblog.utils.CommonUtils;
import edu.yctc.weblog.utils.IPUtil;
import edu.yctc.weblog.utils.PageQueryUtil;
import edu.yctc.weblog.utils.VOUtil;
import edu.yctc.weblog.vo.CheckDoneVO;
import edu.yctc.weblog.vo.CommentVO;

@RestController
@RequestMapping(URLConstant.S + URLConstant.COMMENT)
public class CommentRestControllerImpl implements CommentRestController {

    @Autowired
    private CommentService commentService;

    @Override
    @GetMapping(URLConstant.COMMENT_GET_COMMENT_LIST)
    public ResultDO<PageDO> getCommentList(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || params.get("limit") == null) {
            return new ResultDO<PageDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        return commentService.getCommentsPage(new PageQueryUtil(params));
    }

    @Override
    @PostMapping(URLConstant.COMMENT_CHECK_DONE)
    public ResultDO<Void> checkDone(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return commentService.checkDone(ids);
    }

    @Override
    @PostMapping(URLConstant.COMMENT_REPLY)
    public ResultDO<Void> reply(@RequestBody CheckDoneVO checkDoneVO) {
        if (checkDoneVO.getCommentId() <= 0 || StringUtils.isBlank(checkDoneVO.getReplyBody())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return commentService.reply(checkDoneVO.getCommentId(), checkDoneVO.getReplyBody());
    }

    @Override
    @PostMapping(URLConstant.COMMENT_DELETE)
    public ResultDO<Void> delete(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return commentService.deleteBatch(ids);
    }

    @Override
    @PostMapping(URLConstant.BLOG_COMMENT)
    public ResultDO<Void> comment(HttpServletRequest request, HttpSession session, @RequestBody CommentVO commentVO) {
        if (StringUtils.isBlank(commentVO.getVerifyCode())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.EMPTY_VERIFY_CODE);
        }
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isBlank(kaptchaCode)) {
            return new ResultDO<>(false, ResultCode.REQUEST_INVALID, ResultCode.MSG_REQUEST_INVALID);
        }
        if (kaptchaCode.equals(commentVO.getVerifyCode()) == false) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.ERROR_VERIFY_CODE);
        }
        String ref = request.getHeader("Referer");
        if (StringUtils.isBlank(ref)) {
            return new ResultDO<>(false, ResultCode.REQUEST_INVALID, ResultCode.MSG_REQUEST_INVALID);
        }
        if (commentVO.getBlogId() == null || commentVO.getBlogId() <= 0) {
            return new ResultDO<>(false, ResultCode.REQUEST_INVALID, ResultCode.MSG_REQUEST_INVALID);
        }
        if (StringUtils.isBlank(commentVO.getCommentator())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.EMPTY_COMMENTATOR);
        }
        if (StringUtils.isBlank(commentVO.getEmail())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.EMPTY_EMAIL);
        }
        if (CommonUtils.isEmailAddress(commentVO.getEmail()) == false) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.INVALID_EMAIL);
        }
        if (StringUtils.isBlank(commentVO.getCommentBody())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.EMPTY_COMMENT);
        }
        if (commentVO.getCommentBody().trim().length() > 200) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.COMMENT_OVER_SIZE);
        }
        CommentDO commentDO = VOUtil.coverCommentVO2CommentDO(commentVO);
        commentDO.setIp(IPUtil.getIpAddr(request));
        commentDO.setStatus(CommentStatus.UNREVIEWED);
        commentDO.setIsDeleted(CommentDeleteStatus.UNDELETED);
        // url过滤
        if (CommonUtils.isURL(commentVO.getWebsiteUrl()) == false) {
            commentDO.setUrl(null);
        }
        return commentService.addComment(commentDO);
    }

}
